﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JZ.Common
{
    public static class Parser
    {
        public static short ParseShort(string value, short? defaultValue)
        {
            short ret;
            if (short.TryParse(value, out ret) == false && defaultValue.HasValue)
            {
                ret = defaultValue.Value;
            }
            return ret;
        }

        public static short ParseShort(string value)
        {
            return ParseShort(value, 0); ;
        }

        public static DateTime ParseDatetime(string value, DateTime? defaultValue)
        {
            DateTime ret;
            if (DateTime.TryParse(value, out ret) == false && defaultValue.HasValue)
            {
                ret = defaultValue.Value;
            }
            return ret;
        }

        public static DateTime ParseDatetime(string value)
        {
            return ParseDatetime(value, DateTime.MinValue); ;
        }

        public static bool ParseBool(string value, bool? defaultValue)
        {
            bool ret;
            if (bool.TryParse(value, out ret) == false && defaultValue.HasValue)
            {
                ret = defaultValue.Value;
            }
            return ret;
        }

        public static bool ParseBool(string value)
        {
            return ParseBool(value, false); ;
        }

        public static float ParseFloat(string value, float? defaultValue)
        {
            float ret;
            if (float.TryParse(value, out ret) == false && defaultValue.HasValue)
            {
                ret = defaultValue.Value;
            }
            return ret;
        }

        public static float ParseFloat(string value)
        {
            return ParseFloat(value,0);
        }


        public static double ParseDouble(string value)
        {
            double ret;
            if (double.TryParse(value, out ret) == false)
            {
                ret = 0;
            }
            return ret;
        }

        public static int ParseInt(string value)
        {
            int ret;
            if (int.TryParse(value, out ret) == false)
            {
                ret = 0;
            }
            return ret;
        }

        public static long ParseLong(string value)
        {
            long ret;
            if (long.TryParse(value, out ret) == false)
            {
                ret = 0;
            }
            return ret;
        }
    }
}
